﻿Imports Global_Visas_Business_Layer
Imports System.Data
Imports System.Text
Imports System.ComponentModel

Partial Public Class frmEditPerson

    Private _OldDetails As People.PersonInfo

    Private Sub frmEditPerson_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyUp
        If e.Key = Key.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub AddPerson_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loaded
        Try
            GeneralTasks.ChangeTheme(Me)
            GeneralTasks.EnumerateandFill(Me.LogicalChildren)
            Dim myPerson As People.PersonInfo = People.GetPerson(GlobalCRM.CurrentCandidateID)
            ' Dim objDT As DataTable
            Dim strProdType As String = CommonDataFunctions.GetCaseProductType(GlobalCRM.CurrentCaseID)

            Me._OldDetails = myPerson

            With Me.drpTitle
                .DisplayMemberPath = "title_text"
                .SelectedValuePath = "title_id"

                .ItemsSource = CType(CommonDataFunctions.TitleList(myPerson.Title), IListSource).GetList
            End With

            With Me.drpDest
                .DisplayMemberPath = "country_name"
                .SelectedValuePath = "country_id"
                .ItemsSource = CType(CommonDataFunctions.CountryList, IListSource).GetList
            End With

            With Me.drpRes
                .DisplayMemberPath = "country_name"
                .SelectedValuePath = "country_id"
                .ItemsSource = CType(CommonDataFunctions.CountryList, IListSource).GetList
            End With

            With Me.drpNat
                .DisplayMemberPath = "country_nationality"
                .SelectedValuePath = "country_id"
                .ItemsSource = CType(CommonDataFunctions.NationalityList, IListSource).GetList
            End With

            With Me.drpUKVisa
                .DisplayMemberPath = "ukvisa_name"
                .SelectedValuePath = "ukvisa_id"
                '.DataSource = CommonSQL.AddExtraRowToDataTable(CommonSQL.UKVisaList, -1, "")
                .ItemsSource = CType(CommonDataFunctions.UKVisaList, IListSource).GetList
            End With

            With Me.drpRole
                .DisplayMemberPath = "role_name"
                .SelectedValuePath = "role_id"
                .ItemsSource = CType(CommonDataFunctions.RoleList, IListSource).GetList
            End With

            With Me.drpQual
                .DisplayMemberPath = "qual_name"
                .SelectedValuePath = "qual_id"
                .ItemsSource = CType(CommonDataFunctions.QualificationList, IListSource).GetList
            End With

            'With Me.drpAccMan
            '    .DisplayMemberPath = "con_name"
            '    .SelectedValuePath = "con_id"
            '    .ItemsSource = CType(CommonDataFunctions.ConsultantList, IListSource).GetList
            'End With

            With Me.drpWhereHeard
                .DisplayMemberPath = "wherehear_text"
                .SelectedValuePath = "wherehear_id"
                .ItemsSource = CType(CommonDataFunctions.WhereHearList, IListSource).GetList
            End With

            With myPerson
                Me.txtSurname.Text = .Surname
                Me.txtFirstName.Text = .Firstname
                Me.drpTitle.Text = .Title
                If .DOB.Year > 1900 Then
                    Me.txtDOB.Text = Format(.DOB, "dd-MMM-yyyy")
                Else
                    Me.txtDOB.Text = String.Empty
                End If
                'Me.drpAccMan.SelectedValue = .AccountManagerId
                Me.drpWhereHeard.Text = .WhereHeard
                Me.drpDest.SelectedValue = .DestinationId
                Me.drpNat.SelectedValue = .NationalityId
                Me.drpRes.SelectedValue = .ResidenceId
                Me.drpUKVisa.SelectedValue = .UKVisaId
                If .UKVisaExpiry.Year > 1900 Then
                    Me.txtVisaExpiry.Text = Format(.UKVisaExpiry, "dd-MMM-yyyy")
                Else
                    Me.txtVisaExpiry.Text = String.Empty
                End If
                Me.drpRole.SelectedValue = .CompanyRoleId
                Me.txtJobTitle.Text = .JobTitle
                Me.drpQual.Text = .Qualification
                Me.txtSalary.Text = .Salary
                Me.txtPassportNo.Text = .PassportNo
                If .PassportExp.Year > 1900 Then
                    Me.txtPassportExpiry.Text = Format(.PassportExp, "dd-MMM-yyyy")
                Else
                    Me.txtPassportExpiry.Text = String.Empty
                End If

                If .SkilledOccupationId > 0 Then
                    Me.chkSkilled.IsChecked = True
                    Me.drpSkillCat.SelectedValue = CommonDataFunctions.GetSkillOccupationCategory(.SkilledOccupationId)
                    Me.drpSkillOcc.SelectedValue = .SkilledOccupationId
                End If
                If .EnableLogin = 1 Then
                    chkLogin.IsChecked = True
                Else
                    chkLogin.IsChecked = False
                End If
            End With

            If strProdType <> "GBR-GBR" Then
                'drpUKVisa.Enabled = False

            End If

            ' if person belongs to a company AccountMgr and WhereHeard is not applicable (this is stored against the company)
            ' if person is private individual Role is not applicable
            If GlobalCRM.CurrentCompanyID > 0 Then
                Me.lblRole.Visibility = Windows.Visibility.Visible
                Me.drpRole.Visibility = Windows.Visibility.Visible
                Me.drpAccMan.Visibility = Windows.Visibility.Hidden
                ' Me.lblAccManager.Visibility = Windows.Visibility.Hidden
                Me.lblWhereHeard.Visibility = Windows.Visibility.Hidden
                Me.drpWhereHeard.Visibility = Windows.Visibility.Hidden
            Else
                Me.lblRole.Visibility = Windows.Visibility.Hidden
                Me.drpRole.Visibility = Windows.Visibility.Hidden
                Me.drpAccMan.Visibility = Windows.Visibility.Hidden
                'Me.lblAccManager.Visibility = Windows.Visibility.Visible
                Me.lblWhereHeard.Visibility = Windows.Visibility.Visible
                Me.drpWhereHeard.Visibility = Windows.Visibility.Visible
            End If
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmEditPerson.Load")
        Finally
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub QuickNatButtons(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAU.Click, btnNZ.Click, btnSA.Click, btnUK.Click, btnCA.Click

        Dim myBtn As Button = CType(sender, Button)

        Me.drpDest.SelectedValue = myBtn.Tag
        Me.drpNat.SelectedValue = myBtn.Tag
        Me.drpRes.SelectedValue = myBtn.Tag

    End Sub

    Private Sub btnCal1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCal1.Click
        Me.txtDOB.Text = CommonFuncs.GetPopUpCalendarDate(Me.txtDOB.Text)
        'frmCalendarPopUp.ShowDialog()
        'Me.txtDOB.Text = Format(GlobalCRM.DatePopUpDate, "dd-MMM-yyyy")
    End Sub

    Private Sub btnCal2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCal2.Click
        Me.txtVisaExpiry.Text = CommonFuncs.GetPopUpCalendarDate(Me.txtVisaExpiry.Text)
        'frmCalendarPopUp.ShowDialog()
        'Me.txtVisaExpiry.Text = Format(GlobalCRM.DatePopUpDate, "dd-MMM-yyyy")
    End Sub

    Private Sub btnCal3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCal3.Click
        Me.txtPassportExpiry.Text = CommonFuncs.GetPopUpCalendarDate(Me.txtPassportExpiry.Text)
        'frmCalendarPopUp.ShowDialog()
        'Me.txtPassportExpiry.Text = Format(GlobalCRM.DatePopUpDate, "dd-MMM-yyyy")
    End Sub


    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim cmn As New CommonFuncs
        Try

            Dim Exp As Boolean
            Dim strProdType As String = CommonDataFunctions.GetCaseProductType(GlobalCRM.CurrentCaseID)

            If Me.drpWhereHeard.Visibility = Windows.Visibility.Visible And Me.drpWhereHeard.SelectedIndex = -1 Then
                If MsgBox("How the person heard about Global Visas could not be added to the database. Do you wish to continue?", MsgBoxStyle.YesNo, "Error") = MsgBoxResult.No Then
                    Me.drpWhereHeard.Focus()
                    Exit Sub
                End If
                'MsgBox("Where Heard cannot be blank", MsgBoxStyle.Exclamation, "Error")
                'Me.drpWhereHeard.Focus()
                'Exit Sub
            End If

            If Me.drpRole.Visibility = Windows.Visibility.Visible And Me.drpRole.SelectedIndex = -1 Then
                CommonFuncs.DisplayMessage("You must select a role for this person", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.drpRole.Focus()
                Exit Sub
            End If

            If Me.drpNat.SelectedIndex = -1 Then
                CommonFuncs.DisplayMessage("You must enter a Nationality", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.drpNat.Focus()
                Exit Sub
            End If

            If Me.drpRes.SelectedIndex = -1 Then
                CommonFuncs.DisplayMessage("You must enter country of residence", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.drpRes.Focus()
                Exit Sub
            End If

            If Me.drpDest.SelectedIndex = -1 Then
                CommonFuncs.DisplayMessage("You must enter a Destination. For UK citizens not migrating select UK.", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.drpDest.Focus()
                Exit Sub
            End If

            If strProdType = "GBR-GBR" Then
                If Me.drpUKVisa.SelectedIndex = -1 Then
                    CommonFuncs.DisplayMessage("You must enter a UK visa status (or select 'none')", CommonDialog.CustomDialogIcons.Information, "Error")
                    Me.drpUKVisa.Focus()
                    Exit Sub
                End If
            End If

            If Me.txtPassportExpiry.Text.Trim <> "" And Not IsDate(Me.txtPassportExpiry.Text) Then
                CommonFuncs.DisplayMessage("Please enter a valid passport expiry", CommonDialog.CustomDialogIcons.Information, "Expiry")
                Exit Sub
            End If


            If Me.txtDOB.Text.Trim <> "" And Not IsDate(Me.txtDOB.Text) Then
                CommonFuncs.DisplayMessage("Please enter a valid date of birth", CommonDialog.CustomDialogIcons.Information, "DOB")
                Exit Sub
            End If


            If strProdType = "GBR-GBR" Then
                If Me.txtVisaExpiry.Text.Trim <> "" And Not IsDate(Me.txtVisaExpiry.Text) Then
                    CommonFuncs.DisplayMessage("Please enter a valid UK visa expiry date", CommonDialog.CustomDialogIcons.Information, "Expiry")
                    Exit Sub
                End If
                If Me.txtVisaExpiry.Text.Trim <> "" Then
                    If CDate(Me.txtVisaExpiry.Text) < cmn.FormatDate(Now) Then
                        Dim checkdate As Integer
                        checkdate = MsgBox("Visa Expiry date is in the past. Are you sure to continue?", MsgBoxStyle.YesNo, "Expiry Date in Past")
                        If checkdate <> 6 Then
                            txtVisaExpiry.Focus()
                            Exit Sub
                        End If
                    End If

                    If Me.txtVisaExpiry.Text.Trim <> "" Then
                        Exp = True
                    End If
                End If
            End If

            If chkSkilled.Visibility = Windows.Visibility.Visible And Me.chkSkilled.IsChecked = True And Me.drpSkillOcc.SelectedIndex = -1 Then
                CommonFuncs.DisplayMessage("Please select a skilled occupation, or uncheck the box", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.chkSkilled.Focus()
                Exit Sub
            End If

            'if Nat, Res or UKVisa values are being changed then validate.

            If (Me.drpNat.SelectedValue <> Me._OldDetails.NationalityId) Or (Me.drpRes.SelectedValue <> Me._OldDetails.ResidenceId) Or (Me.drpUKVisa.SelectedValue <> Me._OldDetails.UKVisaId) Then

                Dim Ret As Integer

                Ret = CommonDataFunctions.GblFunc_Validate_NatResVisa(Me.drpNat.SelectedValue, Me.drpRes.SelectedValue, Me.drpUKVisa.SelectedValue, Exp, 1)

                Select Case Ret

                    Case 0
                        'Ok

                    Case 1, 2, 3
                        Exit Sub

                    Case 4
                        'ok
                    Case 5
                        CommonFuncs.DisplayMessage("Cannot enter an unknown value for Nat, Residence or Visa type, please amend", CommonDialog.CustomDialogIcons.Information, "Error")
                        Exit Sub

                    Case 10
                        CommonFuncs.DisplayMessage("Error validating", CommonDialog.CustomDialogIcons.Information, "Error")
                        Exit Sub

                End Select
            End If



            Dim myPerson As New People.PersonInfo

            With myPerson
                .PersonId = GlobalCRM.CurrentCandidateID
                .Surname = Me.txtSurname.Text.Trim
                .Firstname = Me.txtFirstName.Text.Trim
                .Title = Me.drpTitle.Text
                If IsDate(Me.txtDOB.Text) Then
                    .DOB = Me.txtDOB.Text.Trim
                End If
                '.AccountManagerId = Me.drpAccMan.SelectedValue
                .WhereHeard = Me.drpWhereHeard.Text
                .DestinationId = Me.drpDest.SelectedValue
                .NationalityId = Me.drpNat.SelectedValue
                .ResidenceId = Me.drpRes.SelectedValue
                .UKVisaId = Me.drpUKVisa.SelectedValue
                If IsDate(Me.txtVisaExpiry.Text) Then
                    .UKVisaExpiry = Me.txtVisaExpiry.Text
                End If
                .CompanyRoleId = Me.drpRole.SelectedValue
                .JobTitle = Me.txtJobTitle.Text.Trim
                .Salary = Me.txtSalary.Text.Trim
                .Qualification = Me.drpQual.Text.Trim
                .PassportNo = Me.txtPassportNo.Text.Trim
                If IsDate(Me.txtPassportExpiry.Text) Then
                    .PassportExp = Me.txtPassportExpiry.Text
                End If
                If Me.chkSkilled.IsChecked Then
                    .SkilledOccupationId = Me.drpSkillOcc.SelectedValue
                Else
                    .SkilledOccupationId = 0
                End If
                If CInt(chkLogin.IsChecked) = -1 Then
                    .EnableLogin = 1 '.ToString
                Else
                    .EnableLogin = 0
                End If
            End With


            If People.AddPerson(myPerson, Me._OldDetails) Then

                Me.Close()
            End If
        Catch ex As Exception
        Finally
            cmn = Nothing
        End Try

    End Sub

    Private Sub drpDest_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles drpDest.SelectionChanged

        With Me.drpSkillCat
            .SelectedValuePath = "SkilledCategory_ID"
            .DisplayMemberPath = "SkilledCategory_Name"
            .ItemsSource = CType(CommonDataFunctions.SkilledOccupationCategories(Me.drpDest.SelectedValue), IListSource).GetList
            .SelectedIndex = -1
        End With

        If Me.drpSkillCat.Items.Count > 0 Then
            'Me.drpSkillCat.Visible = True
            'Me.drpSkillOcc.Visible = True
            'Me.lblSkillCat.Visible = True
            'Me.lblSkillOcc.Visible = True
            Me.chkSkilled.Visibility = Windows.Visibility.Visible
        Else
            'Me.drpSkillCat.Visible = False
            'Me.drpSkillOcc.Visible = False
            'Me.lblSkillCat.Visible = False
            'Me.lblSkillOcc.Visible = False
            Me.chkSkilled.Visibility = Windows.Visibility.Hidden
        End If

    End Sub

    Private Sub chkSkilled_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSkilled.Checked
        'If Me.chkSkilled.IsChecked Then
        Me.drpSkillCat.Visibility = Windows.Visibility.Visible
        Me.drpSkillOcc.Visibility = Windows.Visibility.Visible
        Me.lblSkillCat.Visibility = Windows.Visibility.Visible
        Me.lblSkillOcc.Visibility = Windows.Visibility.Visible
        'Else
        '    Me.drpSkillCat.Visibility = Windows.Visibility.Hidden
        '    Me.drpSkillOcc.Visibility = Windows.Visibility.Hidden
        '    Me.lblSkillCat.Visibility = Windows.Visibility.Hidden
        '    Me.lblSkillOcc.Visibility = Windows.Visibility.Hidden
        'End If
    End Sub

    Private Sub drpSkillCat_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles drpSkillCat.SelectionChanged
        With Me.drpSkillOcc
            .SelectedValuePath = "SkilledOcc_ID"
            .DisplayMemberPath = "SkilledOcc_Title"
            .ItemsSource = CType(CommonDataFunctions.SkilledOccupationList(Me.drpSkillCat.SelectedValue), IListSource).GetList
            .SelectedIndex = -1
        End With
    End Sub

    Private Sub txtVisaExpiry_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim strDate As String

        If txtVisaExpiry.Text = String.Empty Then Exit Sub

        If IsDate(txtVisaExpiry.Text) = False Then
            If MsgBox("The date entered is incorrect, click ok to re-enter or cancel to set to current date.", MsgBoxStyle.Question + MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
                txtVisaExpiry.Focus()
            Else
                txtVisaExpiry.Text = CStr(Now.Day()) & "-" & Format(Now, "MMM") & "-" & CStr(Now.Year())
            End If
        Else
            strDate = CDate(txtVisaExpiry.Text).Day & "-" & Format(CDate(txtVisaExpiry.Text), "MMM") & "-" & CDate(txtVisaExpiry.Text).Year
            txtVisaExpiry.Text = strDate
        End If

    End Sub

    Private Sub txtDOB_LEave(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim strDate As String

        If txtDOB.Text = String.Empty Then Exit Sub

        If IsDate(txtDOB.Text) = False Then
            If MsgBox("The date entered is incorrect, click ok to re-enter or cancel to set to current date.", MsgBoxStyle.Question + MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
                txtDOB.Focus()
            Else
                txtDOB.Text = CStr(Now.Day()) & "-" & Format(Now, "MMM") & "-" & CStr(Now.Year())
            End If
        Else
            strDate = CDate(txtDOB.Text).Day & "-" & Format(CDate(txtDOB.Text), "MMM") & "-" & CDate(txtDOB.Text).Year
            txtDOB.Text = strDate
        End If

    End Sub

    Private Sub txtPassportExpiry_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)


        Dim strDate As String

        If txtPassportExpiry.Text = String.Empty Then Exit Sub

        If IsDate(txtPassportExpiry.Text) = False Then
            If MsgBox("The date entered is incorrect, click ok to re-enter or cancel to set to current date.", MsgBoxStyle.Question + MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
                txtPassportExpiry.Focus()
            Else
                txtPassportExpiry.Text = CStr(Now.Day()) & "-" & Format(Now, "MMM") & "-" & CStr(Now.Year())
            End If
        Else
            strDate = CDate(txtPassportExpiry.Text).Day & "-" & Format(CDate(txtPassportExpiry.Text), "MMM") & "-" & CDate(txtPassportExpiry.Text).Year
            txtPassportExpiry.Text = strDate
        End If

    End Sub


    Private Sub chkSkilled_Unchecked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles chkSkilled.Unchecked
        Me.drpSkillCat.Visibility = Windows.Visibility.Hidden
        Me.drpSkillOcc.Visibility = Windows.Visibility.Hidden
        Me.lblSkillCat.Visibility = Windows.Visibility.Hidden
        Me.lblSkillOcc.Visibility = Windows.Visibility.Hidden
    End Sub

    Private Sub txtDOB_SelectedDateChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles txtDOB.SelectedDateChanged
        Me.txtDOB.Text = (New CommonFuncs).FormatDate(txtDOB.SelectedDate)
    End Sub

    Private Sub txtPassportExpiry_SelectedDateChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles txtPassportExpiry.SelectedDateChanged
        Me.txtPassportExpiry.Text = (New CommonFuncs).FormatDate(txtPassportExpiry.SelectedDate)
    End Sub

    Private Sub txtVisaExpiry_SelectedDateChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles txtVisaExpiry.SelectedDateChanged
        Me.txtVisaExpiry.Text = (New CommonFuncs).FormatDate(txtVisaExpiry.SelectedDate)
    End Sub
End Class
